package crystal.workshop;

import java.io.File;
import java.util.Arrays;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;

import mujl.io.FileReaderManager;

import crystal.lib.StructureData;

import crystal.workshop.io.DiskReader;
import crystal.workshop.io.LocalDiskStructureInfo;
import crystal.workshop.ui.SimpleStructureList;
import crystal.workshop.ui.StructureDataModel;


//public class LocalDiskSource implements StructureSource {
//	
//	protected final DiskReader reader;
//	
//	protected SimpleStructureList structuresList = new SimpleStructureList();
//	
//	private String name	= "Local disk";
//	
//	public LocalDiskSource(final DiskReader reader) {
//		this.reader = reader;
//	}
//	
//	@Override
//	public String getName() {
//		return name;
//	}
//	
//	@Override
//	public void updateAll() {
//		// Update the structures
//		for( int i = 0; i < structuresList.getSize(); ++i ) {
//			update( structuresList.getStructure(i) );
//		}
//	}
//
//	@Override
//	public boolean update(StructureDataModel struct) {
//		return reader.update(struct);
//	}
//
//	@Override
//	public int getNumberOfStructures() {
//		return structuresList.getSize();
//	}
//	
//	@Override
//	public Collection<StructureDataModel> getStructures() {
//		return structuresList.getStructures();
//	}
//	
//	/**
//	 * Load a structure file from disk.
//	 * @param strFile the structure file to load.
//	 */
//	public void load(final File strFile) {
//		// TODO Add some checking here, maybe an exception thrown
//		StructureDataModel str = reader.loadStructure(strFile);
//		
//		structuresList.add(str, this);
//	}
//	
//	public void remove(final StructureDataModel str) {
//		reader.unloadStructure(str);
//		structuresList.remove(str);
//	}
//	
//	/**
//	 * This method does three things:<br/>
//	 * 1) Loads any new structure in <code>strFiles</code>
//	 * 2) Removes any structures that aren't in <code>strFiles</code>
//	 * 3) Updates all structures that are not up to date
//	 * @param strFiles the list of structure files to synchronise with.
//	 */
//	public void syncWith(final File[] strFiles) {
//		
//		// Get all the .res files in the search directory
//		final LinkedList<File> syncFiles = new LinkedList<File>(Arrays.asList(strFiles));
//		final LinkedList<File> oldFiles = new LinkedList<File>(Arrays.asList(getStructureFiles()));
// 
//		// Now match up which ones we already have
//		File currentFile = null;
//		for( final Iterator<File> iter = syncFiles.iterator(); iter.hasNext();
//		currentFile = iter.next()) {
//			if(oldFiles.contains(currentFile)) {
//				// Remove the file from the both lists
//				oldFiles.remove(currentFile);
//				iter.remove();
//			}
//		}
//		
//		// Now the ones that are left in syncFiles as new and the ones left in
//		// oldFiles are to be deleted
//		
//		// So delete old ones
//		for( final Iterator<File> iter = oldFiles.iterator(); iter.hasNext();
//		currentFile = iter.next()) {
//			remove(currentFile);
//		}
//		
//		// Update the current ones
//		updateAllNoMessage();
//		
//		// Add new ones
//		for( final Iterator<File> iter = syncFiles.iterator(); iter.hasNext();
//		currentFile = iter.next()) {
//			load(currentFile);
//		}
//	}
//	
//	/**
//	 * Get an array of all the files associated with structures from this
//	 * source.  No guarantee is made about the order of the list or how the
//	 * files relate to structures.
//	 * @return
//	 */
//	public File[] getStructureFiles() {
//		final File[] structureFiles = new File[structures.size()];
//		
//		int i = 0;
//		for( final Iterator<LocalDiskStructureInfo> iter = structures.values().iterator(); iter.hasNext(); ) {
//			structureFiles[i] = iter.next().getStructureFile();
//			++i;
//		}
//		
//		return structureFiles;
//	}
//	
//	/**
//	 * Private helper function that updates all structures but doesn't dispatch
//	 * an update message afterwards.  Usually this would be used if the calling
//	 * function was deferring sending the message till later.
//	 */
//	private void updateAllNoMessage() {
//		updateAll();
//	}
//
//}